<?php


namespace app\admin\system\controller;


use app\admin\system\logic\DictDataLogic;
use app\common\controller\BaseAdminController;
use core\constants\CacheKey;
use core\utils\CacheUtil;
use core\utils\DictUtil;

class DictDataController extends BaseAdminController
{
    protected $noNeedRight = ['*'];


    protected function init()
    {
        $this->logic = new DictDataLogic();
        parent::init();
    }

    protected function searchWhere()
    {
        $where = [];
        $this->form['name'] && $where[] = ['name', 'like', "%{$this->form['name']}%"];
        $this->form['dict_type'] && $where[] = ['dict_type', '=', $this->form['dict_type']];
        $this->form['status'] && $where[] = ['status', '=', $this->form['status']];
        return $where;
    }

    public function listDataByType()
    {
        $dict_type = input('dict_type');
        return $this->success(DictUtil::getList($dict_type));
    }

    public function read()
    {
        $model = $this->logic->find(request()->input('id'));
        if ($model) {
            return $this->success($model->toArray());
        } else {
            return $this->error('未查找到信息');
        }
    }

    public function afterUpdate($type, &$data)
    {
        CacheUtil::tag(CacheKey::DICT_TAG)->clear();
    }
}